<?php

declare (strict_types=1);

namespace app\store\controller\market;

use think\Db;
use think\response\Json;
use app\store\controller\Controller;
use app\store\model\ExchangeCode as ExchangeCodeModel;
//use app\store\service\Coupon as CouponService;

/**
 * 兑换码管理
 * Class Coupon
 * @package app\store\controller\market
 */
class ExchangeCode extends Controller
{
    /**
     * 列表记录
     * @return Json
     * @throws \think\db\exception\DbException
     */
    public function list(): Json
    {
        $model = new ExchangeCodeModel();
        $list = $model->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 详情记录
     * @param int $codeId
     * @return Json
     */
    public function detail(int $codeId): Json
    {
        $detail = ExchangeCodeModel::detail($codeId);
        return $this->renderSuccess(compact('detail'));
    }

    /**
     * 添加兑换码
     * @return Json
     */
    public function add(): Json
    {
        // 新增记录
        $model = new ExchangeCodeModel;
        if ($model->add($this->postForm())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 更新兑换码库
     * @param int $couponId
     * @return Json
     */
    public function edit(int $codeId): Json
    {
        // 兑换码详情
        $model = ExchangeCodeModel::detail($codeId);
        // 更新记录
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除兑换码
     * @param int $codeId
     * @return Json
     */
    public function delete(string $codeIds): Json
    {
        $model = new ExchangeCodeModel();
        if($model->deleteByIds($codeIds)){
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError($model->getError() ?: '删除失败');
    }

}
